export default {
  // 通过 props 为复用逻辑提供初始值
  props: {
    initialLikes: { type: Number, default: 0 },
    initialViews: { type: Number, default: 0 },
    title: { type: String, default: '演示卡片' }
  },
  // 可复用的响应式数据
  data() {
    return {
      likes: 0,
      views: 0,
      createdAt: new Date()
    }
  },
  // 生命周期：混入与组件都会执行，混入通常先于组件执行
  created() {
    this.likes = this.initialLikes
    this.views = this.initialViews
    // 为了教学展示效果，打印日志
    console.log('[statsMixin] created:', { likes: this.likes, views: this.views })
  },
  // 复用计算属性
  computed: {
    popularityScore() {
      return this.likes * 2 + this.views
    },
    ageSeconds() {
      return Math.floor((Date.now() - this.createdAt.getTime()) / 1000)
    }
  },
  // 复用方法
  methods: {
    incrementLike() {
      this.likes++
    },
    incrementView() {
      this.views++
    },
    // 数字格式化：可在组件内用同名方法覆盖，演示合并策略
    formatNumber(n) {
      if (n >= 1000) return (n / 1000).toFixed(1) + 'K'
      return String(n)
    }
  },
  // 复用侦听逻辑
  watch: {
    likes(newVal, oldVal) {
      console.log(`[statsMixin] likes changed: ${oldVal} -> ${newVal}`)
    }
  }
}